package com.ztc.ZtcWithdrawalsApply.service;

import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.Pager;
import com.ztc.AppUser.bean.AppUser;
import com.ztc.AppUser.dao.AppUserDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.dao.ZtcTransportDao;
import com.ztc.ZtcTransportCapitalLog.bean.ZtcTransportCapitalLog;
import com.ztc.ZtcTransportCapitalLog.dao.ZtcTransportCapitalLogDao;
import com.ztc.ZtcWallet.bean.ZtcWallet;
import com.ztc.ZtcWallet.dao.ZtcWalletDao;
import com.ztc.ZtcWithdrawalsApply.bean.ZtcWithdrawalsApply;
import com.ztc.ZtcWithdrawalsApply.dao.ZtcWithdrawalsApplyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service("ZtcWithdrawalsApply")
public class ZtcWithdrawalsApplyManager{

    @Autowired
    ZtcWithdrawalsApplyDao ztcWithdrawalsApplyDao;
    @Autowired
    AppUserDao appUserDao;
    @Autowired
    ZtcTransportDao ztcTransportDao;
    @Autowired
    ZtcWalletDao ztcWalletDao;
    @Autowired
    ZtcTransportCapitalLogDao ztcTransportCapitalLogDao;


    public String saveWithdrawalsApply(ZtcWithdrawalsApply ztcWithdrawalsApply){
        ztcWithdrawalsApplyDao.save(ztcWithdrawalsApply);
        return ztcWithdrawalsApply.getId();
    }

    public ZtcWithdrawalsApply get(String id){
        return ztcWithdrawalsApplyDao.get(id);
    }

    public List<ZtcWithdrawalsApply> findBy(String propertyName1, Object value1, String propertyName2, Object value2) {
        return ztcWithdrawalsApplyDao.findBy(propertyName1, value1, propertyName2, value2);
    }

    public List<ZtcWithdrawalsApply> findBy(String propertyName, Object value) {
        return ztcWithdrawalsApplyDao.findBy(propertyName, value);
    }

    /**
     * 获取所有待处理提现列表.
     * @return
     */
    public HashMap<String,Object> getAllWillOfZtcWithdrawalsApply(Integer pageSize,Integer pageNumber){
        //获取所有待处理提现列表
        Pager<ZtcWithdrawalsApply> ztcWithdrawalsApplyList= ztcWithdrawalsApplyDao.getAllWillOfZtcWithdrawalsApply(pageSize,pageNumber);
        List<HashMap<String,Object>> result=new ArrayList<>();
        return getStringObjectHashMap(ztcWithdrawalsApplyList.getContent(), result,ztcWithdrawalsApplyList.getRecordTotal());
    }


    /**提现账户类型（1支付宝  2 微信）*/
    private String getTransferAccountType(Integer transferAccountType){
        if (transferAccountType==null){
            return null;
        }
        switch (transferAccountType){
            case 1:return "支付宝";
            case 2:return "微信";
            default:return null;
        }
    }

    /**业务类型：1-用户 2-公司*/
    private String getBusinessType(Integer businessType){
        if (businessType==null){
            return null;
        }
        switch (businessType){
            case 1:return "用户提现";
            case 2:return "公司提现";
            default:return null;
        }
    }

    /**
     * 当前公司钱包余额.
     * @param user 当前公司用户
     * @return
     */
    public HashMap<String,Object> getWalletMoney(User user){
        ZtcTransport ztcTransport=ztcTransportDao.get(user.getTransportId());
        if(ztcTransport==null){
            return JsonWrapper.failureWrapperMsg("未查询到公司信息！");
        }
        ZtcWallet ztcWallet=ztcWalletDao.findUniqueBy("fkUserId",ztcTransport.getId());
        if(ztcWallet==null){
            return JsonWrapper.failureWrapperMsg("未查询到公司账户信息！");
        }
        HashMap<String,BigDecimal> result=new HashMap<>();
        result.put("money",ztcWallet.getMoney());
        result.put("cashMoney",ztcWallet.getTmoney());
        return JsonWrapper.successWrapper(result);
    }

    /**
     * 当前公司提交提现申请.
     * @param  money    提现金额
     * @param  ztcWallet   公司钱包
     * @param  ztcTransportId  当前公司ID
     * @return
     */
    @Transactional
    public HashMap<String,Object> withdrawals(ZtcWallet ztcWallet,BigDecimal money,String ztcTransportId,Integer accountType,String transferAccount){
        ZtcWithdrawalsApply ztcWithdrawalsApply = new ZtcWithdrawalsApply();
        ztcWithdrawalsApply.setBusinessId(ztcTransportId);
        ztcWithdrawalsApply.setMoney(money);
        ztcWithdrawalsApply.setStatus(1);
        ztcWithdrawalsApply.setTransferAccountType(accountType);
        ztcWithdrawalsApply.setTransferAccount(transferAccount);
        ztcWithdrawalsApply.setBusinessType(2);//公司提现
        ztcWithdrawalsApply.setAddTime(new Date());
        String id = ztcWithdrawalsApplyDao.saveWithdrawalsApply(ztcWithdrawalsApply);
        if(id!=null){
            BigDecimal bigDecimal=ztcWallet.getMoney().subtract(money);//余额
            BigDecimal bigDecimal2=ztcWallet.getTmoney().subtract(money);//可提现金额
            ztcWallet.setMoney(bigDecimal);
            ztcWallet.setTmoney(bigDecimal2);
            ztcWalletDao.update(ztcWallet);
            ZtcTransportCapitalLog ztcTransportCapitalLog =new ZtcTransportCapitalLog();
            ztcTransportCapitalLog.setBusinessId(ztcWithdrawalsApply.getId());
            ZtcTransport transport = new ZtcTransport();
            transport.setId(ztcWithdrawalsApply.getBusinessId());
            ztcTransportCapitalLog.setZtcTransport(transport);
//            ztcTransportCapitalLog.setTransportId(ztcWithdrawalsApply.getBusinessId());
            ztcTransportCapitalLog.setAfterBalance(bigDecimal);
            ztcTransportCapitalLog.setCreateTime(new Date());
            ztcTransportCapitalLog.setType("1");
            ztcTransportCapitalLog.setMoney(money.multiply(new BigDecimal(-1)));
            ztcTransportCapitalLog.setRemark("公司提现");
            ztcTransportCapitalLogDao.save(ztcTransportCapitalLog);
            return JsonWrapper.successWrapper("提现申请成功，请等待处理！");
        }
        return JsonWrapper.failureWrapperMsg("提现申请失败，请稍后重试！");
    }

    /**
     * 获取当前公司所有提现列表
     * @param transportId   公司ID
     * @return
     */
    public HashMap<String,Object> getAllZtcWithdrawalsApply(String transportId,Integer pageSize,Integer pageNumber,Integer type) {
        Pager<ZtcWithdrawalsApply> ztcWithdrawalsApplyList= ztcWithdrawalsApplyDao.getAllWillOfZtcWithdrawalsApply(transportId,pageSize,pageNumber,type);
        List<HashMap<String,Object>> result=new ArrayList<>();
        return getStringObjectHashMap(ztcWithdrawalsApplyList.getContent(), result,ztcWithdrawalsApplyList.getRecordTotal());
    }

     /** 提现列表字段 */
    private HashMap<String, Object> getStringObjectHashMap(List<ZtcWithdrawalsApply> ztcWithdrawalsApplyList, List<HashMap<String, Object>> result,Integer size) {
        HashMap<String, Object> map;
        if(ztcWithdrawalsApplyList.size()>0){
            for (ZtcWithdrawalsApply ztcWithdrawalsApply:ztcWithdrawalsApplyList) {
                map=new HashMap<>();
                map.put("id",ztcWithdrawalsApply.getId());
                map.put("businessType",getBusinessType(ztcWithdrawalsApply.getBusinessType()));
                map.put("money",ztcWithdrawalsApply.getMoney());
                map.put("transferAccountType",getTransferAccountType(ztcWithdrawalsApply.getTransferAccountType()));
                map.put("transferAccount",ztcWithdrawalsApply.getTransferAccount());
                map.put("status",getCashStatus(ztcWithdrawalsApply.getStatus()));
                map.put("addTime",ztcWithdrawalsApply.getAddTime());
                //app用户提现主体
                if(ztcWithdrawalsApply.getBusinessType()==1){
                    AppUser user=  appUserDao.get(ztcWithdrawalsApply.getBusinessId());
                    if (user!=null){
                        map.put("cashBodyName",user.getName());
                    }else{
                        map.put("cashBodyName","");
                    }
                }
                //公司提现主体
                if(ztcWithdrawalsApply.getBusinessType()==2){
                    ZtcTransport ztcTransport=ztcTransportDao.get(ztcWithdrawalsApply.getBusinessId());
                    if (ztcTransport!=null){
                        map.put("cashBodyName",ztcTransport.getFullName());
                    }else{
                        map.put("cashBodyName","");
                    }
                }
                result.add(map);
            }
        }
        return JsonWrapper.successPagerWrapper(result,size);
    }

    /**提现状态（状态：1-进行中;2-完成;3-失败）*/
    private String getCashStatus(Integer cashStatus){
        if (cashStatus==null){
            return null;
        }
        switch (cashStatus){
            case 1:return "待处理";
            case 2:return "完成";
            case 3:return "失败";
            default:return null;
        }
    }
}
